import { Component, Injector, OnInit } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import {

    BadWordsServiceProxy, ConfigFileDto
} from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { IAjaxResponse, TokenService } from 'abp-ng2-module';
import { finalize } from 'rxjs/operators';

@Component({
    templateUrl: './bad-words.component.html',
    animations: [appModuleAnimation()]
})
export class BadWordsComponent extends AppComponentBase implements OnInit {

    badWords: string;
    loading = false;

    constructor(
        injector: Injector,
        private _badWordsServiceProxy: BadWordsServiceProxy,
        private _tokenService: TokenService
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this.getBadWords();
    }

    getBadWords() {
        this.loading = true;
        this._badWordsServiceProxy.getConfigFileContent()
            .pipe(finalize(() => {
                this.loading = false;
            }))
            .subscribe((result: string) => {
                this.badWords = result;
            });
    }

    saveAll() {
        let input = new ConfigFileDto({ content: this.badWords });

        this.loading = true;
        this._badWordsServiceProxy.updateOrCreateConfigFile(input)
            .pipe(finalize(() => {
                this.loading = false;
            }))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
            });
    }
}
